"""
@author: XDing
@file: 猜拳游戏.py
@time: 2023/4/19 21:24
"""
import random

class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.players = []

    def add_player(self, player):
        if len(self.players) < 2:
            self.players.append(player)
            player.room = self
            print(f"{player.name} 加入房间： {self.room_id}")
        else:
            print("房间已满，请重新选择")

    def remove_player(self, player):
        if player in self.players:
            self.players.remove(player)
            player.room = None
            print(f"{player.name} 离开了房间： {self.room_id}")
        else:
            print("玩家不在该房间")

class Player:
    def __init__(self, player_id, name, gold):
        self.player_id = player_id
        self.name = name
        self.gold = gold
        self.room = None

    def play(self):
        if self.room is None:
            print("您不在房间中！")
            return
        # 人数小于2
        if len(self.room.players) < 2:
            print("等待其他玩家加入")
            return

        print(f"{self.name}, 输入剪刀(c), 石头(s), 布(p):")
        choice = input().lower()

        if choice not in ['c', 's', 'p']:
            print("输入有误，请重试！")
            return

        other_player = self.room.players[0] if self.room.players[0] != self else self.room.players[1]
        other_choice = random.choice([ 'c', 's', 'p'])

        print(f"{self.name} 出 {choice}, {other_player.name} 出 {other_choice}.")

        if choice == other_choice:
            print("平局！")
        elif (choice == 's' and other_choice == 'p') or (choice == 'p' and other_choice == 'c') or (choice == 'c' and other_choice == 's'):
            print(f"{self.name} 获胜！")
            self.gold += 100
            other_player.gold -= 100
        else:
            print(f"{other_player.name} 获胜！")
            self.gold -= 100
            other_player.gold += 100

        if self.gold <= 0:
            print(f"{self.name} 输光了，请退出充值！")
            self.room.remove_player(self)
        elif other_player.gold <= 0:
            print(f"{other_player.name}输光了，请推出充值！")
            self.room.remove_player(other_player)

        print(f"{self.name} 拥有 {self.gold} 金豆")


user1 = Player( "001", "XX",  1000)
user2 = Player( "002", "DD",  1000)

room = Room(1)

while 1:
    print("欢迎来到猜拳游戏~".center(20,"*"))
    c1 = input("1.加入房间  2.离开房间 请输入您的选择(按q退出):")
    if c1 == "q":
        break
    if c1 in "wertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM":
        room.add_player(user1)
        room.add_player(user2)
        user1.play()
        user2.play()




